<?php
// require LivraOAuth lib

require_once('../LivraOAuth.php');
require_once('../LivraClient.php');


/* Sessions are used to keep OAuthAutentication Object with Livra */
session_start();


/* Set up placeholder */
$content = NULL;


/* Clear PHP sessions */
if ($_REQUEST['test'] === 'clear') {/*{{{*/
  session_destroy();
  session_start();
}/*}}}*/




$_SESSION['client']  = new LivraClient($oauthClient); 

if(isset($_SESSION['client'])){
    $client = $_SESSION['client'];
}

if ($_REQUEST['methodToCall'] == 'getPick') {
    $param = $_REQUEST['parameter'];
    $arrayParams =  explode(",",$param);
    $xml = $client->getPick(array($arrayParams[0] => $arrayParams[1] )); 
    
}



$categories = $client->getCategories(); 

if($_REQUEST['methodToCall'] == 'vote'){

 $client->votePick(array('option_id'=>$_REQUEST['option_id']));

}

if($_REQUEST['methodToCall'] == 'getPicks'){
    $picks = $client->getPicks(array( 'criterion' => $_REQUEST['criterion'], 'category_id' => $_REQUEST['category_id'])); 

}



 
 ?>
 
 
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Demo - Livra API Tester</title>
        <link href="../css/styles.css" rel="stylesheet" type="text/css" media="all" />
        <link rel="shortcut icon" type="image/x-icon" href="http://livra.com/rsrc/favicon.ico"/>
        
    </head>

    <body>
        <div id="container">
            <div id="header">
                <h1>
                    <img src="../img/livralogo.gif" border="0" />
                    <span>API</span>
                </h1>
            </div>
            
            <div id="mainbody">
                <h2>This is a demo of the Livra API for developers.</h2>
                <div class="content">
                    <div class="methods">Chose a criteria and a Category and navigate the Picks</div>
                    
                    <ul>
                       
                       <li>Get Picks by criteria 
                       
                           <form method="GET" style="display:inline">
                                <input type="hidden" name="methodToCall" value="getPicks"/>
                                <select name="criterion">
                                    <?
                                    foreach ($client->getCriterions() as $k => $v) {
                                        echo "<option value=\"" . $k . "\">". $v ."</option>";
                                    }
                                    ?>
                                </select>
                                Select a Category: 
                                <?
                                if($categories != null && $categories != false){
                                    if(isset($categories->childs)){
                                     echo "<select name=\"category_id\">";
                                    foreach ($categories->childs->child as $child){
                                            echo "<option value=\"" . $child->id . "\">"; 
                                            echo $child->name;
                                            echo "</option>";
                                    }
                                    echo "</select>";
                                    }
                    
                                }
                                
                                ?>
                                
                                
                                <input type="submit" value="get picks"></input>
                            </form>
                       </li>
                    </ul>           
                </div>     

                <? 
                    if($xml != null && $xml != false){
                        if(isset($xml->options)){
                            echo "<table>";
                            echo '<tr> Pick question: '. substr($xml->question, 0, strlen($xml->question)>=10?10:strlen($xml->question)) . ' Author: ' . $xml->user->name . '</tr>';
                            foreach ($xml->options->option as $option){ 
                                echo "<tr><td>"; 
                                echo $option->name;
                                echo "</td>";
                                echo "</td>";
                                echo "<td> " .  round($option->vote_count / $xml->vote_count  * 100 ,2). " % </td>";
                                echo "</td>";
                                echo "<td><img src=\"" . $option->image . "\"  width=\"100\" height=\"100\" /></td>";
                                echo "</tr>";
                            }
                            echo "</table>";
                        }
                    }
                    
                    if($picks != null && $picks != false){
                        if(isset($picks->picks)){
                            echo "<table>";
                            foreach($picks->picks->pick as $xml){
                            
                            echo '<tr><td> Pick question: <a href="' . $_SERVER['PHP_SELF'] . '?methodToCall=getPick&parameter=id,' . $xml->id . '">'. substr($xml->question, 0, strlen($xml->question)>=30?30:strlen($xml->question)) . '</a> Author: ' . $xml->user->name . '</td></tr>';
                                
                            
                            }
                            echo "</table>";
                        }
                    }
                    
                    ?>
            </div>
            
            <div id="footer">
                <span>Livra.com</span>
            </div>
        </div>
    </body>
</html>
 